package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.domain.Product;
import csu.web.mypetstore.service.AccountService;
import csu.web.mypetstore.service.CatalogService;
import csu.web.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class SignOnServlet extends HttpServlet {

    private static final String SIGN_ON_FORM = "/WEB-INF/jsp/account/signon.jsp";

    private String username;
    private String password;
    private String vCode;

    private String msg;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.username = req.getParameter("username");
        this.password = req.getParameter("password");
        this.vCode=req.getParameter("vCode");
        //HttpSession session = req.getSession();

       //

        //校验用户输入的正确性
        if(!validate()){
            req.setAttribute("signOnMsg", this.msg);
            req.getRequestDispatcher(SIGN_ON_FORM).forward(req,resp);
        }else{
            AccountService accountService = new AccountService();
            Account loginAccount = accountService.getAccount(username, password);
            if(loginAccount == null){
                this.msg = "用户名或密码错误";
                req.setAttribute("signOnMsg", this.msg);
                req.getRequestDispatcher(SIGN_ON_FORM).forward(req,resp);
            }
            else {
                //loginAccount.setPassword(null);
                HttpSession session = req.getSession();
                if(session.getAttribute("code").equals(vCode))
                {
                    session.setAttribute("loginAccount" , loginAccount);

                    if(loginAccount.isListOption()){
                        CatalogService catalogService = new CatalogService();
                        List<Product> myList = catalogService.getProductListByCategory(loginAccount.getFavouriteCategoryId());
                        session.setAttribute("myList", myList);
                    }

                    if(loginAccount != null) {
                        LogService logService = new LogService();
                        String logInfo = logService.logInfo(" ") +"用户" +loginAccount.getUsername()+  "  "+"成功登录";
                        logService.insertLogInfo(loginAccount.getUsername(),logInfo);
                    }

                    resp.sendRedirect("mainForm");
                }
                else{
                    this.msg = "验证码错误";
                    req.setAttribute("signOnMsg", this.msg);
                    req.getRequestDispatcher(SIGN_ON_FORM).forward(req,resp);
                }
            }
        }
    }

    private boolean validate(){
        if(this.username == null || this.username.isEmpty()){
            this.msg = "用户名不能为空";
            return false;
        }
        if(this.password == null || this.password.isEmpty()){
            this.msg = "密码不能为空";
            return false;
        }

        if(this.vCode==null||this.vCode.isEmpty())
        {
            this.msg="验证码不能为空";
            return false;
        }
        return true;
    }

}
